﻿using System;
using System.Web.UI.WebControls;
using Gasco.Business.Services;
using Gasco.DataAccess;

public partial class Controls_contact_index : System.Web.UI.UserControl
{
    private ContactService contactService { get; set; }
    public ContactService ContactService
    {
        get
        {
            if (contactService == null)
                contactService = new ContactService();

            return contactService;
        }
    }

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
        {
            GetContacts(null, 1, true, gvContacts.SortExpression, gvContacts.SortDirection.ToString());
        }
    }

    protected void gvContact_Sorting(object sender, GridViewSortEventArgs e)
    {
        if (ViewState["sort"] == null || ViewState["sort"].ToString() == "asc")
        {
            e.SortDirection = SortDirection.Descending;
            ViewState["sort"] = "desc";
        }
        else
        {
            e.SortDirection = SortDirection.Ascending;
            ViewState["sort"] = "asc";
        }

        GetContacts(tbSearch.Text, gvContacts.PageIndex + 1, false, e.SortExpression, e.SortDirection.ToString());
    }

    protected void gvContacts_Deleting(object sender, GridViewDeleteEventArgs e)
    {
        ContactService cs = new ContactService();
        cs.DeleteContact(Convert.ToInt32(gvContacts.DataKeys[e.RowIndex].Value));
        GetContacts(tbSearch.Text, 1, true, gvContacts.SortExpression, gvContacts.SortDirection.ToString());
    }

    private void GetContacts(string keyword, int pageNumber, bool setPageCount, string sortBy, string desc)
    {
        ViewState["CurrentPage"] = pageNumber;

        if (setPageCount)
            gvContacts.VirtualItemCount = ContactService.CountSearchContacts(keyword);

        gvContacts.DataSource = ContactService.SearchContacts(keyword, gvContacts.PageSize, pageNumber, sortBy, desc);
        gvContacts.DataBind();
    }

    public string FindPathForShow(int type_id)
    {
        string path = "";
        if (type_id == 1)
        {
            path = "personal_contact_view";
        }
        else
        {
            path = "company_contact_view";
        }
        return path;
    }
    
    public string FindPath(int type_id)
    {
        string path = "";
        if (type_id == 1)
        {
            path = "personal_contact_add";
        }
        else
        {
            path = "company_contact_add";
        }
        return path;
    }

    public string FindField(int id,string field)
    {
        string tel = "";
        ContactService cs =new ContactService();
        contact c = cs.GetContactById(id) ;
        if (c.contact_type_id == 1)
        {
            switch(field)
            {
                case "Tel":
                    tel = c.personal_contact.work_tel;
                    break;
                case "Mobile":
                    tel=c.personal_contact.mobile;
                    break;
            }
            
        }
        else
        {
            switch(field)
            {
                case "Tel":
                  tel = c.company_contact.ceo_tel;
                  break;
                case "Mobile":
                  tel = c.company_contact.ceo_mobile;
                  break;
            }
            
        }
        return tel;
    }
    
    protected void gvContacts_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        gvContacts.PageIndex = e.NewPageIndex;
        GetContacts(tbSearch.Text, e.NewPageIndex + 1, false, gvContacts.SortExpression, gvContacts.SortDirection.ToString());
    }

    protected void lbSearch_Click(object sender, EventArgs e)
    {
        GetContacts(tbSearch.Text, 1, true, gvContacts.SortExpression, gvContacts.SortDirection.ToString());
    }
}
